<?php namespace Home\Controller; 
use Hdphp\Controller\Controller;

//列表页控制器
class ListController extends Controller{
	public function index(){
		$cateModel=new \Common\Model\Cate;
		$tagModel=new \Common\Model\Tag;
		$arcModel=new \Common\Model\Arc;
		$arcTagModel=new \Common\Model\ArcTag;
		//点击分类
		if($cid=Q('get.cid',0,'intval')){
			$name="分类";
			$value=$cateModel->where("cid={$cid}")->pluck('cname');
			//获得所有的子集
			$cids=$cateModel->getSon($cateModel->get(),$cid);
			$cids[]=$cid;
			//统计该分类及分类子集下的文章总数
			$total=$arcModel->where('category_cid IN ('.implode(',', $cids).")" )->where('is_recycle=0')->count();
			//查询分类对应的文章
			$data=$arcModel
					->join('category','category_cid','=','cid')
					->where('category_cid IN ('.implode(',', $cids).")")
					->where('is_recycle=0')
					->get();
			foreach($data as $k=>$v){
				$data[$k]['tag']=$arcTagModel
					->join('tag','tag_tid','=','tid')
					->where("article_aid={$v['aid']}")
					->get();
			}
		}
		//点击标签
		if($tid=Q('get.tid',0,'intval')){
			$name="标签";
			$value=$tagModel->where("tid={$tid}")->pluck('tname');
			$total=$arcTagModel
					->join('article','article_aid','=','aid')
					->where('is_recycle=0')
					->where("tag_tid={$tid}")
					->count();
			//获取当前标签下的文章
			$data=$arcTagModel
					->join('article','article_aid','=','aid')
					->join('category','category_cid','=','cid')
					->where('is_recycle=0')
					->where("tag_tid={$tid}")
					->get();
					
			foreach($data as $k=>$v){
				$data[$k]['tag']=$arcTagModel
					->join('tag','tag_tid','=','tid')
					->where("article_aid={$v['aid']}")
					->get();
			}
		}
		//点击搜索
		if($select=Q('post.select')){
			$name=$select;
			$data=$arcModelModel
					->join('article_data','aid','=','article_aid')
					->get();
			p($data);
		}
//		p($data);
		View::with('name',$name);
		View::with('value',$value);
		View::with('total',$total);
		View::with('data',$data);
//		p($data);
		View::make();
	}
}
 ?>